#!/bin/bash

set -e

COMMIT_MSG="up"

while getopts ":m:" opt; do
  case "${opt}" in
    m)
      COMMIT_MSG="${OPTARG}"
      ;;
    \?)
      echo "用法: $0 [-m commit_message]"
      exit 1
      ;;
  esac
done

shift $((OPTIND - 1))

if [[ -z "$(git status --porcelain)" ]]; then
  echo "没有需要提交的改动。"
  exit 0
fi

echo "Add changes..."
git add -A

if git diff --cached --quiet; then
  echo "暂存区无改动，跳过提交。"
  exit 0
fi

echo "Commit with message: ${COMMIT_MSG}"
git commit -m "${COMMIT_MSG}" -n

echo "Push..."
git push

echo "Done."

